package com.chinackts.customsdeclarations.query;

import com.alibaba.fastjson.JSONObject;
import com.chinackts.common.exception.ApplicationException;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;

/**
 * @author Lianhong
 * @since 2022/4/6
 */
public class CustomsDeclarationsPageListQuery extends CustomsDeclarationsListQuery {

    @ApiModelProperty("页码")
    @NotNull(message = "页码不能为空")
    private Integer pageNo;

    @ApiModelProperty("分页条数")
    @NotNull(message = "分页条数不能为空")
    @Max(value = 500, message = "请提供范围在500以内的分页条数")
    private Integer pageSize;

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public JSONObject buildPageQueries() {
//        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMdd").withZoneUTC();
//        DateTime start = formatter.parseDateTime(getStartDate());
//        DateTime end = formatter.parseDateTime(getEndDate());
//        Days days = Days.daysBetween(start.toLocalDate(), end.toLocalDate());
//        if (days.getDays() > 90) {
//            throw new ApplicationException("9998", "开始与结束日期必须在90天内");
//        }
        if (StringUtils.isBlank(getDecType())) {
            throw new ApplicationException("9998", "请提供报关单类型");
        }
        JSONObject result = new JSONObject();
        result.put("curPage", pageNo);
        result.put("pageSize", pageSize);
        result.put("decType", getDecType());
        if (StringUtils.isNotEmpty(getEntryId())) {
            result.put("entryId", getEntryId());
            result.put("startDate", StringUtils.EMPTY);
            result.put("endDate", StringUtils.EMPTY);
        } else {
            result.put("startDate", getStartDate());
            result.put("endDate", getEndDate());
        }
        return result;
    }
}
